package cn.iocoder.yudao.module.member.agency.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.eligibility.service.AppEligibilityBaseService;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.agency.enums.AgencyLevelEnum;
import cn.iocoder.yudao.module.api.member.agency.vo.AgencyApplyVO;
import cn.iocoder.yudao.module.member.agency.dal.dataobject.AgencyApplyDO;
import cn.iocoder.yudao.module.member.agency.dal.dataobject.AgencyOrderDO;
import cn.iocoder.yudao.module.member.agency.dal.dataobject.AgencyUserDO;
import cn.iocoder.yudao.module.member.agency.dal.mysql.AgencyUserMapper;
import cn.iocoder.yudao.module.member.agency.mq.AgencyProducer;
import cn.iocoder.yudao.module.member.agency.vo.AppAgencyUserVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 招商人员信息 Service 实现类
 *
 * @author 山野羡民
 */
@Slf4j
@Service
@Validated
public class AppAgencyUserServiceImpl extends AppEligibilityBaseService<AgencyUserDO, AgencyOrderDO, AppAgencyUserVO> implements AppAgencyUserService {
    @Resource
    private AgencyUserMapper agencyUserMapper;

    @Resource
    private AgencyProducer agencyProducer;

    @Nonnull
    @Override
    protected AgencyUserDO buildForInsert(@Nonnull Long userId) {
        return new AgencyUserDO();
    }

    @Nonnull
    @Override
    protected AgencyUserDO buildForUpdate(@Nonnull AgencyOrderDO order, @Nonnull AgencyUserDO aDo) {
        AgencyUserDO agencyUserDO = super.buildForUpdate(order, aDo);
        agencyUserDO.setLevel(order.getLevel());
        return agencyUserDO;
    }

    @Override
    protected AgencyUserMapper getInjectedMapper() {
        return agencyUserMapper;
    }

    @Override
    public boolean isValid(Long userId, @Nullable AgencyLevelEnum level) {
        try {
            AgencyUserDO agencyUserDO = getOrCreate(userId);
            if (level == null) {
                return agencyUserDO.isValid();
            }
            return agencyUserDO.isValid() && Objects.equals(level.getValue(), agencyUserDO.getLevel());
        } catch (Exception e) {
            log.error("isValid error", e);
            return false;
        }
    }

    @Override
    public void updateWhenAuditPassed(AgencyApplyDO aDo) {
        AgencyUserDO entity = getOrCreate(aDo.getUserId());
        AgencyUserDO updateObj = new AgencyUserDO();
        updateObj.setId(entity.getId());
        updateObj.setContactName(aDo.getContactName());
        updateObj.setContactMobile(aDo.getContactMobile());
        getInjectedMapper().updateById(updateObj);
        agencyProducer.sendAgencyAuditPassMessage(BeanUtils.toBean(aDo, AgencyApplyVO.class));
    }

    @Override
    public int agencyExpireBySystem() {
        CoreLambdaQueryWrapper<AgencyUserDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.ne(AgencyUserDO::getLevel, AgencyLevelEnum.NONE.getValue());
        wrapper.le(AgencyUserDO::getExpireTime, LocalDateTime.now());
        List<AgencyUserDO> list = getInjectedMapper().selectList(wrapper);
        if (CollUtil.isEmpty(list)) {
            return 0;
        }
        List<AgencyUserDO> updateList = new ArrayList<>();
        for (AgencyUserDO agencyUserDO : list) {
            agencyProducer.sendAgencyUserExpiredMessage(agencyUserDO.getUserId());
            AgencyUserDO updateObj = new AgencyUserDO();
            updateObj.setId(agencyUserDO.getId());
            updateObj.setLevel(AgencyLevelEnum.NONE.getValue());
            updateList.add(updateObj);
        }
        getInjectedMapper().updateById(updateList);
        return updateList.size();
    }

}